﻿Imports System.Xml.Serialization

Namespace Aricie.DNN.Services.Url.FriendlyUrl

    <Serializable()> _
    Public Class TemplateCaptures

        Public Sub New(ByVal friendlyMapping As FriendlyUrlMapping)
            Me._Mapping = friendlyMapping
        End Sub

        Private _Mapping As FriendlyUrlMapping

        <NonSerialized()> Private _Regex As Regex
        <NonSerialized()> Private _Match As Match
        '<NonSerialized()> Private _FriendlyPrefixCaptures As CaptureCollection
        '<NonSerialized()> Private _FriendlyRegexGroups As GroupCollection
        '<NonSerialized()> Private _FriendlyLastPart As String = Nothing
        <NonSerialized()> Private _RealUrlLastPart As String = Nothing
        <NonSerialized()> Private _RealUrlRegexPositions As IList(Of Integer)
        <NonSerialized()> Private _RealUrlPrefixes As IList(Of String)

        <XmlIgnore()> _
        Public ReadOnly Property Mapping() As FriendlyUrlMapping
            Get
                Return _Mapping
            End Get
        End Property


        <XmlIgnore()> _
        Public ReadOnly Property Regex() As Regex
            Get
                If _Regex Is Nothing Then
                    _Regex = New Regex(Me._Mapping.Pattern, RegexOptions.CultureInvariant Or RegexOptions.Compiled Or RegexOptions.IgnoreCase)
                End If
                Return _Regex
            End Get
        End Property

        <XmlIgnore()> _
        Public ReadOnly Property Match() As Match
            Get
                If Me._Match Is Nothing Then
                    Me._Match = Me.Regex.Match(Me._Mapping.FriendlyUri.AbsoluteUri)
                End If
                Return Me._Match
            End Get
        End Property

        '''' <summary>
        '''' Retourne une collection des captures des préfixes
        '''' </summary>
        '''' <value></value>
        '''' <returns></returns>
        '''' <remarks></remarks>
        '<XmlIgnore()> _
        'Public ReadOnly Property FriendlyPrefixCaptures() As CaptureCollection
        '    Get
        '        If _FriendlyPrefixCaptures Is Nothing Then
        '            _FriendlyPrefixCaptures = Me.Match.Groups(FriendlierUrlContext.TEMPLATE_PREFIX_GROUP_NAME).Captures
        '        End If
        '        Return _FriendlyPrefixCaptures
        '    End Get
        'End Property

        '''' <summary>
        '''' Retourne une collection des captures des parties variables
        '''' </summary>
        '''' <value></value>
        '''' <returns></returns>
        '''' <remarks></remarks>
        '<XmlIgnore()> _
        'Public ReadOnly Property FriendlyRegexGroups() As GroupCollection
        '    Get
        '        If _FriendlyRegexGroups Is Nothing Then
        '            '_FriendlyRegexCaptures = Me.Match.Groups(FriendlierUrlContext.TEMPLATE_REGEX_GROUP_NAME).Captures
        '            _FriendlyRegexCaptures = Me.Match.Groups(FriendlierUrlContext.TEMPLATE_REGEX_GROUP_NAME).Captures
        '        End If
        '        Return _FriendlyRegexCaptures
        '    End Get
        'End Property

        '<XmlIgnore()> _
        'Public ReadOnly Property FriendlyLastPart() As String
        '    Get
        '        If _FriendlyLastPart Is Nothing Then
        '            Dim lastGroup As Group = Me.Match.Groups(FriendlierUrlContext.TEMPLATE_LASTPART_GROUP_NAME)
        '            If Not lastGroup.Success Then
        '                _FriendlyLastPart = ""
        '            Else
        '                _FriendlyLastPart = lastGroup.Value
        '            End If
        '        End If
        '        Return _FriendlyLastPart
        '    End Get
        'End Property

        <XmlIgnore()> _
        Public ReadOnly Property RealUrlRegexPositions() As IList(Of Integer)
            Get
                If Me._RealUrlRegexPositions Is Nothing Then
                    Dim regexRangeIndexInRealUrl As New SortedList(Of Integer, Integer)
                    For i As Integer = 1 To Me.Match.Groups.Count - 1
                        'first group is global match, skip it
                        Dim objGroup As Group = Me.Match.Groups(i)
                        regexRangeIndexInRealUrl.Add(Me._Mapping.OriginalUri.AbsoluteUri.IndexOf(objGroup.Value, StringComparison.OrdinalIgnoreCase), i)
                    Next
                    Me._RealUrlRegexPositions = regexRangeIndexInRealUrl.Values

                End If
                Return Me._RealUrlRegexPositions

            End Get
        End Property

        <XmlIgnore()> _
        Public ReadOnly Property RealUrlPrefixes() As IList(Of String)
            Get
                If Me._RealUrlPrefixes Is Nothing Then
                    Me._RealUrlPrefixes = New List(Of String)
                    Dim realUrl As String = Me._Mapping.OriginalUri.AbsoluteUri
                    Dim cursor As Integer = 0
                    Dim captureIndex As Integer
                    Dim objGroup As Group
                    For i As Integer = 0 To Me.Match.Groups.Count - 2
                        objGroup = Me.Match.Groups(RealUrlRegexPositions(i))
                        captureIndex = realUrl.IndexOf(objGroup.Value, StringComparison.OrdinalIgnoreCase)
                        Me._RealUrlPrefixes.Add(realUrl.Substring(cursor, captureIndex - cursor))
                        cursor = captureIndex + objGroup.Length
                    Next

                End If
                Return Me._RealUrlPrefixes
            End Get
        End Property

        <XmlIgnore()> _
        Public ReadOnly Property RealUrlLastPart() As String
            Get
                If _RealUrlLastPart Is Nothing Then
                    Dim realUrl As String = Me._Mapping.OriginalUri.AbsoluteUri
                    Dim lastRegexValue As String = Me.Match.Groups(Me.RealUrlRegexPositions(Me.Match.Groups.Count - 2)).Value
                    _RealUrlLastPart = realUrl.Substring(realUrl.IndexOf(lastRegexValue, StringComparison.OrdinalIgnoreCase) + lastRegexValue.Length)
                End If

                Return _RealUrlLastPart
            End Get
        End Property

    End Class

End Namespace


